﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using GalaSoft.MvvmLight;

using Stock.Service;
using Stock.Model;
using Stock.Util;
using Stock.Core;
using GalaSoft.MvvmLight.Command;
using HandyControl.Tools.Extension;
using HandyControl.Data;
using System.Windows.Controls;
using HandyControl.Tools;
using HandyControl.Controls;
using System.Windows;

namespace Stock.ViewModel
{
    public class AppConfigViewModel : ViewModelBase, IDialogResultable<AppConfigModel>
    {
        public AppConfigViewModel()
        {
            Init();
        }

        public List<KeyValueModel> AutoStartList { get; set; }

        public List<KeyValueModel> SkinList { get; set; }

        public List<KeyValueModel> InfoTypeList { get; set; }

        public List<KeyValueModel> DialogList { get; set; }

        public List<KeyValueModel> InfoPositionList { get; set; }

        public List<KeyValueModel> SadSwichList { get; set; }

        public List<KeyValueModel> HappySwichList { get; set; }

        private AppConfigModel result;
        public AppConfigModel Result { get => result; set => Set(ref result, value); }
        public Action CloseAction { get; set; }

        private string zhangColor;
        private string dieColor;
        private string spriteBgColor;

        public string ZhangColor { get => zhangColor; set => Set(ref zhangColor, value); }

        public string DieColor { get => dieColor; set => Set(ref dieColor, value); }

        public string SpriteBgColor { get => spriteBgColor; set => Set(ref spriteBgColor, value); }

        public RelayCommand<string> SetColorCmd => new RelayCommand<string>(SetColor);

        public RelayCommand CancelCmd => new RelayCommand(Cancel);

        public RelayCommand ConfirmCmd => new RelayCommand(Confirm);

        public RelayCommand ResetCmd => new RelayCommand(Reset);

        private void Init()
        {
            AutoStartList = new List<KeyValueModel>()
            {
                new KeyValueModel(1,AppConst.TEXT_YES),
                new KeyValueModel(0,AppConst.TEXT_NO)
            };

            SkinList = new List<KeyValueModel>
            {
                new KeyValueModel((int)SkinType.Default, AppConst.TEXT_SKIN_0),
                new KeyValueModel((int)SkinType.Dark, AppConst.TEXT_SKIN_1),
                new KeyValueModel((int)SkinType.Violet, AppConst.TEXT_SKIN_2)
            };

            InfoTypeList = new List<KeyValueModel>()
            {
                new KeyValueModel((int)StockInfoType.Growl,AppConst.TEXT_INFO_TYPE_GROWL),
                new KeyValueModel((int)StockInfoType.Sprite,AppConst.TEXT_INFO_TYPE_SPRITE)
            };

            DialogList = new List<KeyValueModel>
            {
                new KeyValueModel(1,AppConst.TEXT_YES),
                new KeyValueModel(0,AppConst.TEXT_NO)
            };

            InfoPositionList = new List<KeyValueModel>()
            {
                new KeyValueModel((int)GrowlInfoPosition.RightTop,AppConst.TEXT_INFO_POSITION_0),
                new KeyValueModel((int)GrowlInfoPosition.RightMiddle,AppConst.TEXT_INFO_POSITION_1),
                new KeyValueModel((int)GrowlInfoPosition.RightButtom,AppConst.TEXT_INFO_POSITION_2)
            };

            SadSwichList = new List<KeyValueModel>
            {
                new KeyValueModel(1,AppConst.TEXT_OPEN),
                new KeyValueModel(0,AppConst.TEXT_CLOSE)
            };

            HappySwichList = new List<KeyValueModel>
            {
                new KeyValueModel(1,AppConst.TEXT_OPEN),
                new KeyValueModel(0,AppConst.TEXT_CLOSE)
            };
        }

        private void Confirm()
        {
            Result.ZhangColor = zhangColor;
            Result.DieColor = dieColor;
            Result.SpriteBgColor = spriteBgColor;

            Result.Valid();

            CloseAction?.Invoke();
        }

        private void Reset()
        {
            Result = new AppConfigModel();
            ChangeColor();
            CloseAction?.Invoke();
        }

        private void SetColor(string type)
        {
            var picker = SingleOpenHelper.CreateControl<ColorPicker>();
            picker.SelectedBrush = Application.Current.GetColorBrush(GetBrushKey(type));

            var window = new PopupWindow
            {
                PopupElement = picker,
                WindowStartupLocation = WindowStartupLocation.CenterScreen,
                AllowsTransparency = true,
                WindowStyle = WindowStyle.None,
                MinWidth = 0,
                MinHeight = 0,
                Title = AppConst.TEXT_COLOR_PICKER
            };

            picker.Confirmed += (s, e) =>
            {
                ChangeColor(type, e.Info);
                window.Close();
            };

            picker.Canceled += (s, e) =>
            {
                window.Close();
            };
            window.Show();
        }

        private void ChangeColor(string type, System.Windows.Media.Color color)
        {
            string colorString = AppUtil.ColorToHex(color);
            Application.Current.AddOrUpdateColor(type, colorString);
            Application.Current.AddOrUpdateBrush(GetBrushKey(type), color);
            switch (type)
            {
                case AppConst.RES_KEY_DIE_COLOR:
                    DieColor = colorString;
                    break;
                case AppConst.RES_KEY_ZHANG_COLOR:
                    ZhangColor = colorString;
                    break;
                case AppConst.RES_KEY_SPRITE_BG_COLOR:
                    SpriteBgColor = colorString;
                    break;
            }
        }

        private string GetBrushKey(string type)
        {
            string brushKey = "";
            switch (type)
            {
                case AppConst.RES_KEY_DIE_COLOR:
                    brushKey = AppConst.RES_KEY_DIE_BRUSH;
                    break;
                case AppConst.RES_KEY_ZHANG_COLOR:
                    brushKey = AppConst.RES_KEY_ZHANG_BRUSH;
                    break;
                case AppConst.RES_KEY_SPRITE_BG_COLOR:
                    brushKey = AppConst.RES_KEY_SPRITE_BG_BRUSH;
                    break;
            }
            return brushKey;
        }

        private void ChangeColor()
        {
            if (!Result.ZhangColor.Equals(ZhangColor))
            {
                Application.Current.AddOrUpdateColor(AppConst.RES_KEY_ZHANG_COLOR, Result.ZhangColor);
                Application.Current.AddOrUpdateBrush(GetBrushKey(AppConst.RES_KEY_ZHANG_COLOR), Result.ZhangColor);
            }
            if (!Result.DieColor.Equals(DieColor))
            {
                Application.Current.AddOrUpdateColor(AppConst.RES_KEY_DIE_COLOR, Result.DieColor);
                Application.Current.AddOrUpdateBrush(GetBrushKey(AppConst.RES_KEY_DIE_COLOR), Result.DieColor);
            }
            if (!Result.SpriteBgColor.Equals(SpriteBgColor))
            {
                Application.Current.AddOrUpdateColor(AppConst.RES_KEY_SPRITE_BG_COLOR, Result.SpriteBgColor);
                Application.Current.AddOrUpdateBrush(GetBrushKey(AppConst.RES_KEY_SPRITE_BG_COLOR), Result.SpriteBgColor);
            }
        }

        private void Cancel()
        {
            ChangeColor();
            Result = null;
            CloseAction?.Invoke();
        }
    }
}
